<?php
include 'lib.php';
ini_set('display_errors', 1);
ini_set('max_execution_time', 500);
$br = '<br/>';

$artists = Database::query("SELECT * FROM `artists` WHERE CHAR_LENGTH(`lastfm_url`) > 0 ORDER BY RAND() LIMIT 50");
ini_set('max_execution_time', 500);

// Include the API
require 'lib/lastfmapi/lastfmapi.php';


// Put the auth data into an array
$authVars = array(
	'apiKey' => '5e6a9eb30f4dd80779c96113d27b5a25',
    'secret' => '1e0a1293702d76020603e622453b620b'
);
$config = array(
	'enabled' => true,
	'path' => 'lib/lastfmapi/',
	'cache_length' => 1800
);
// Pass the array to the auth class to eturn a valid auth
$auth = new lastfmApiAuth('setsession', $authVars);

// Call for the album package class with auth data
$apiClass = new lastfmApi();
$artistClass = $apiClass->getPackage($auth, 'artist', $config);

foreach ($artists as $artist)
{
    $artist_tags = Database::find('object_tags', array('object_id' => $artist['id'], 'object_type' => 'artist'));
    echo $br."<b>Artist: ".$artist['name'].'</b> has '.count($artist_tags).' tags.'.$br;
    if (count($artist_tags) >= 3) continue;
    // Setup the variables
    $methodVars = array(
    	'artist' => $artist['name'],
    );
    
    if ($tags = $artistClass->getTopTags($methodVars) ) {
    	echo '<b>Data Returned</b>';
        //print_r($tags);
    }
    else {
    	echo '<b>Error '.$artistClass->error['code'].' - </b><i>'.$artistClass->error['desc'].'</i>';
        continue;
    }
    
    
   
        
    foreach ($tags as $i=>$tag_node)
    {
        if (!$tag_node['name']) continue;
        
        $tag = Database::find('tags', array('tag' => strtolower($tag_node['name'])));

    
        if ($tag)
        {
            echo 'Found existing tag: '.$tag[0]['tag'].$br;
            
            if (artistHasTag($artist_tags, strtolower($tag_node['name']))) continue;
            
            $artist_tag = array(
                'tag_id' => $tag[0]['id'],
                'object_type' => 'artist',
                'object_id' => $artist['id']
            );
            

            Database::save('object_tags', $artist_tag);       
        }
        else
        {
                    
            $tag = array(
                'tag' => strtolower($tag_node['name']),
                'source' => 'last.fm',
                'date' => date('Y-m-d H:i:s')
            );
            
            $tag_id = Database::save('tags', $tag); 
            echo 'New tag: '.$tag['tag'].' id: '.$tag_id.$br;
                           
            $artist_tag = array(
                'tag_id' => $tag_id,
                'object_type' => 'artist',
                'object_id' => $artist['id']
            );
            
            $tag_id = Database::save('object_tags', $artist_tag);   
        }
        
        if ($i>=3) break;
    }

}

function artistHasTag($artist_tags, $tag_name)
{
    foreach ($artist_tags as $artist_tag)
    {
        if ($tag['tag'] == strtolower($tag_node['name'])) return true;
    }
    
    return false;
}
?>